#!/bin/bash
#从Trouble,也就是元数据表内获取指定数量的数据,并且导入到目标数据库$tablename中.当数据不足的时候会循环获取补足数据
#注意文件格式

count=`mysql csknow -e "select count(*) as count from Trouble;"|awk '/[0-9]+/ {print $0}'`
dbname=$1
tablename=$2
limit=$3
step=$4
if [[ -z $dbname ]]
then
echo "empty dbname arg 1"
exit
fi
if [[ -z $tablename ]]
then
echo "empty tablename arg 2"
exit
fi
if [[ -z $limit ]]
then
echo "empty limit arg 3"
exit
else
	if [ $limit -gt $count ]
	then
	round=`expr \( $limit / $count \) + 1`
	remain=`expr $limit % $count`
	else
	round=1
	remain=$limit
	fi
fi
if [[ -z $step ]]
then
echo "empty step arg 4"
exit
fi
i=1
while [ $i -le $round ]
do
if [ $i -eq $round ]
then
limit=$remain
else
limit=$count
fi
sql='INSERT INTO '$tablename' (title,`area`,idlist,memo,`time`,staffid,staffname,flag,sflag)
SELECT
  `Title`                      AS `title`,
  AffectArea                   AS `area`,
  AffectServiceGroupInfoIDList AS idlist,
  Memo                         AS `memo`,
  CreateTime                   AS `time`,
  CreateStaffID                AS staffid,
  CreateStaffName              AS staffname,
  EnableFlag                   AS flag,
  SearchSyncFlag               AS sflag
FROM csknow.Trouble limit '$limit';'
echo $sql
mysql $dbname -e "$sql"
i=`expr $i + 1` 
done
#按照主键更新nid
sql='update '$tablename' set `nid`=round(`id`/'$step',0)'
echo $sql
mysql $dbname -e "$sql"
